#include "SessionDetailDialog.h"
#include <QGridLayout>
#include <QPushButton>
#include "ChooseFriendDialog.h"
#include "Custom/LabelButton.h"
#include "Debug.h"
#include "Model/Data.h"
#include <QMessageBox>
#include "Toast.h"
#include "Model/DataCenter.h"
using namespace model;

///////////////////////////////////////////
/// 点击会话右上角的...时弹出的画面
/// 此窗口为好友显示的窗口
///////////////////////////////////////////
/// @param user_info: 是好友的信息
SessionDetailDialog::SessionDetailDialog(UserInfo* user_info, QWidget* parent)
    : QDialog(parent)
{
    this->setWindowTitle("用户信息");
    this->setFixedSize(300, 230);
    this->setWindowIcon(QIcon(":/image/logo.png"));
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setStyleSheet(
        "QWidget {"
        "background-color: #FFFFFF;"
        "}");
    /* 1. 添加按钮 */
    const QIcon  add_icon = QIcon(":/image/cross.png");
    LabelButton* add_btn  = new LabelButton(add_icon, "添加");
    LabelButton* user_btn  = new LabelButton();
    /* 2. 用户的头像 */
#ifdef TEST_UI
    const QIcon  user_icon = QIcon(":/image/vanilla.png");
    user_btn  = new LabelButton(user_icon, "香草123456");
#endif
    user_btn = new LabelButton(user_info->avatar, user_info->nick_name);
    /* 3. 删除好友的按钮 */
    QPushButton* del_btn = new QPushButton();
    del_btn->setFixedSize(170, 40);
    del_btn->setText("删除好友");
    del_btn->setStyleSheet(
        "QPushButton{"
        "background-color: #0099FF;"
        "border: none;"
        "border-radius: 5px;"
        "}"
        "QPushButton:hover {"
        "background-color: #0093F5;"
        "}"
        "QPushButton:pressed {"
        "background-color: #0086E0;"
        "}");
    /* 4. 添加布局 */
    QGridLayout* g_layout = new QGridLayout();
    g_layout->setContentsMargins(0, 0, 0, 0);
    g_layout->setSpacing(0);
    g_layout->setVerticalSpacing(50);
    this->setLayout(g_layout);
    g_layout->addWidget(add_btn, 0, 0, 1, 1);
    g_layout->addWidget(user_btn, 0, 1, 1, 1);
    g_layout->addWidget(del_btn, 1, 0, 1, 3, Qt::AlignHCenter | Qt::AlignTop);
    /* 5. 添加信号槽 */
    connect(add_btn->get_button(), &CircleButton::clicked, this, [=]() {
        ChooseFriendDialog* cf_dialog = new ChooseFriendDialog(user_info, this);
        cf_dialog->exec();
    });
    connect(del_btn, &QPushButton::clicked, this, [=]() {
        /* 这里的逻辑与UserInfoDialog.cpp中点击删除好友的按钮逻辑一致 */
        auto res = QMessageBox::warning(this, "删除好友", "确定删除好友?", QMessageBox::Ok | QMessageBox::Cancel);
        if (res != QMessageBox::Ok) {
            Toast::showMessage("取消删除好友");
            return;
        }
        DataCenter* data_center = DataCenter::get_instance();
        data_center->del_friend_async(user_info->user_id);
        this->close();
    });
}

